<template>
  <view>
    <u-gap height="150"></u-gap>
   <!-- <u-image
      width="100%"
      height="300rpx"
      mode='scaleToFill'
      src="/static/Login.svg"
    ></u-image> -->
    <view style="margin:0  20px;">
      <u-gap height="150"></u-gap>
      <u-form
        :model="form"
        ref="uForm"
        label-width="60"
        class="u-m-40"
      >
        <u-form-item
          label="用户名"
          prop="username"
        >
          <u-input
            placeholder="请输入用户名或手机号"
            v-model="form.username"
          />
        </u-form-item>
        <u-form-item
          label="密码"
          prop="password"
        >
          <u-input
            placeholder="请输入密码"
            type="password"
            v-model="form.password"
          />
        </u-form-item>
      </u-form>
      <u-gap height="30"></u-gap>
      <u-button
        class="u-m-l-40 u-m-40"
        type="primary"
        ripple
        @click="submit"
      > 登 录 </u-button>

      <u-gap height="30"></u-gap>
    <!--  <view
        style="margin:0  20px;color:blue;text-align: right;"
        @click.stop="goIndex"
      >回到首页</view>-->
    </view> 

    <!-- <u-gap height="20"></u-gap> -->
    <!-- <u-button class="u-m-l-40 u-m-r-40" type="primary" ripple @click="login2">登录2</u-button> -->
    <!-- <u-button class="u-m-l-40 u-m-r-40" type="primary" ripple plain @click="">注册</u-button> -->
    <!-- <u-gap height="150"></u-gap> -->
  </view>
</template>

import graceChecker from '@/common/graceChecker.js'
import formRuleConfig from '@/config/formRule.config.js'
<script>
export default {
  data() {
    return {
      // 如果为登录状态 默认跳转的地址
      redirectRoute: this.$mRoutesConfig.index, //请求路径
      routeQuery: {}, //请求参数
      form: {
        username: 'admin',
        password: '123456',
        thirdUserId: '',
        thirdType: ''
      },
      loading: false,
      submitBtnDis: false,
      rules: {
        username: [
          {
            required: true,
            message: '请输入用户名',
            trigger: ['change', 'blur']
          }
        ],
        password: [
          {
            required: true,
            message: '请输入密码',
            trigger: ['change', 'blur']
          }
        ],

      }
    };
  },

  onLoad(query) {
    this.initredirectRouteData(query);
  },
  onReady() {
    this.$refs.uForm.setRules(this.rules);
  },
  onNavigationBarButtonTap() {
    this.goIndex();
  },
  methods: {
    goIndex() {
		console.log("---跳转---")
      // uni.switchTab({
      //   url: '/pages/index/index'
      // });
    },
    initredirectRouteData(query) {
      if (query.redirectUrl) {
        this.redirectRoute.path = query.redirectUrl;
        delete query.redirectUrl;
        this.routeQuery = query;
      }
    },

    gotoPage(path, _this) {
      console.log("gotoPage", path)
      let arr = ["/pages/index/index"]
      if (arr.indexOf(path) >= 0) {
        _this.$mRouter.redirectTo({
          route: { path: path },
          query: _this.routeQuery
        });
      } else {
        _this.$mRouter.redirectTo({
          route: { path: path },
          query: _this.routeQuery
        });
      }
    },

    submit() {

      //进行验证
      let _valid = false;
	  console
	  this.$refs.uForm.validate().then(res => {
		  console.log("///",res)
		  _valid = res;
		  
		  console.log("--->",_valid)
		  if (!_valid) {
		    return false;
		  }
		  let _this = this;
		  let baseur = _this.$mConfig.baseUrl;
		  
		  
		  
		  console.log("-----")
		  _this.$apis.testLogin(_this.form).then((res) => {
		    if (res.success) {
		      console.log(res.result)
		      let result = res.result;
		      _this.$store.commit("SET_USERINFO", result.userInfo || {});
		      _this.$store.commit("SET_USERID", result.userInfo.username);
		      _this.$store.commit("SET_TOKEN", result.token);
			  console.log("+++++",_this.$store)
		      // 前端自动登录
		      // console.log("redirectTo",_this.redirectRoute,_this.routeQuery);
		      _this.gotoPage(_this.redirectRoute.path, _this);
		      // _this.$mRouter.switchTab({
		      // 	route: _this.redirectRoute.path,
		      // 	query: _this.routeQuery
		      // });
		    } else {
		      _this.submitBtnDis = false;
		      uni.showToast({
		        title: res.message,
		        icon: 'none',
		        duration: 1000
		      })
		    }
		  })
		  
	  })
    //   this.$refs.uForm.validate(valid => {
		  // console.log("///",valid)
    //     _valid = valid;
    //   });
		

      // let _this = this;
      // let baseur = _this.$mConfig.baseUrl;
	  
	 

      // uni.getProvider({
      //   service: 'oauth',
      //   success: function (res) {
           
      //       //没有使用微信平台 直接登录
      //       _this.submitBtnDis = true;
      //       _this.$apis.testLogin(_this.form).then((res) => {
      //         if (res.success) {
      //           console.log(res.result)
      //           let result = res.result;
      //           _this.$store.commit("SET_USERINFO", result.userInfo || {});
      //           _this.$store.commit("SET_USERID", result.userInfo.username);
      //           _this.$store.commit("SET_TOKEN", result.token);
      //           // 前端自动登录
      //           // console.log("redirectTo",_this.redirectRoute,_this.routeQuery);
      //           _this.gotoPage(_this.redirectRoute.path, _this);
      //           // _this.$mRouter.switchTab({
      //           // 	route: _this.redirectRoute.path,
      //           // 	query: _this.routeQuery
      //           // });
      //         } else {
      //           _this.submitBtnDis = false;
      //           uni.showToast({
      //             title: res.message,
      //             icon: 'none',
      //             duration: 1000
      //           })
      //         }
      //       });


          
      //   }
      // });

    },

  }
}
</script>

<style lang="scss">
</style>
